package com.joysuch.wwyt.workflow.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.core.enums.Status;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.workflow.bean.WorkFlowProcessBean;
import com.joysuch.wwyt.workflow.entity.WorkFlowTask;
import com.joysuch.wwyt.workflow.entity.WorkflowProcessPrintTemplate;
import com.joysuch.wwyt.workflow.mapper.WorkflowProcessPrintTemplateMapper;
import com.joysuch.wwyt.workflow.repository.WorkFlowTaskDao;
import com.joysuch.wwyt.workflow.service.WorkFlowProcessService;
import com.joysuch.wwyt.workflow.service.WorkflowProcessPrintTemplateService;
import com.joysuch.wwyt.workflow.utils.PrintTemplateContentFillUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jwk
 * @since 2021-06-28
 */
@Service
@Slf4j
public class WorkflowProcessPrintTemplateServiceImpl extends ServiceImpl<WorkflowProcessPrintTemplateMapper, WorkflowProcessPrintTemplate> implements WorkflowProcessPrintTemplateService {

    @Autowired
    private WorkFlowTaskDao workFlowTaskDao;
    @Autowired
    private WorkFlowProcessService workFlowProcessService;

    @Override
    public List<WorkflowProcessPrintTemplate> findTemplateList(Long processId) {
        LambdaQueryWrapper<WorkflowProcessPrintTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WorkflowProcessPrintTemplate::getProcessId, processId);
        wrapper.eq(WorkflowProcessPrintTemplate::getDeleteFlag, "0");
        wrapper.eq(WorkflowProcessPrintTemplate::getTenentId, Context.getCurrentTenantId());
        wrapper.likeRight(WorkflowProcessPrintTemplate::getOrgCode, Context.getCompanyCode());
        wrapper.orderByDesc(WorkflowProcessPrintTemplate::getId);
        return super.list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDefaultTemplate(Long oldDefaultTemplateId, Long newDefaultTemplateId) {
        WorkflowProcessPrintTemplate oldDefaultTemplate = new WorkflowProcessPrintTemplate();
        oldDefaultTemplate.setId(oldDefaultTemplateId);
        oldDefaultTemplate.setDefaultTemplate(0);
        super.updateById(oldDefaultTemplate);
        WorkflowProcessPrintTemplate newDefaultTemplate = new WorkflowProcessPrintTemplate();
        newDefaultTemplate.setId(newDefaultTemplateId);
        newDefaultTemplate.setDefaultTemplate(1);
        super.updateById(newDefaultTemplate);
    }

    @Override
    public JSONObject fillTemplateContent(Long taskId, Long templateId) throws ApiBusinessException {
        WorkflowProcessPrintTemplate template =  super.getById(templateId);
        String templateStr = template.getTemplateJson();
        Optional<WorkFlowTask> optionalTask = workFlowTaskDao.findById(taskId);
        if (optionalTask.isPresent()) {
            WorkFlowTask task = optionalTask.get();
            log.info("printTemplateDataFill template Str: {} ",templateStr);
            log.info("printTemplateDataFill formData Str: {} ",task.getFormData());
            WorkFlowProcessBean workFlowProcessBean = workFlowProcessService.findDetailById(task.getProcessId());
            return PrintTemplateContentFillUtil.printTemplateDataFill(templateStr,task.getFormData(),workFlowProcessBean.getFormJson());
        }else{
            throw new ApiBusinessException(Status.PROCESS_DEPLOY_ERROR);
        }
    }
}
